---
title: "Untitled"
author: "brouwern@gmail.com"
date: "May 5, 2017"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

This script produces AIC values for the appendix.

## Set wd

```{r}
#setwd
my.dir <-  "C:/Users/lisanjie2/Dropbox/Latta_et_al_2017_PeerJ_FINAL"

setwd(my.dir)

```

## Load all model output
```{r}
#file names of output from modeling file
all.output <- c("canopy_ob_output_list.RData"
,"canopy_obYN_list.RData"

,"canopy_use_output_list.RData"
,"canopy_useYN_list.RData"

,"cons_prior_model_list.RData"
,"cons_priority_output_list.RData"

,"dist_sense_model_list.RData"
,"dist_sense_output_list.RData"

,"elev_mig_model_list.RData"
,"elev_mig_output_list.RData"

,"forage_guilde2lev_list.RData"
,"forage_guilde2lev_output_list.RData"

,"habitat_breadt_output_list.RData"
,"hab.breadth_list.RData"

,"forage_guilde3lev_list.RData"
,"forage_guilde3lev_output_list.RData"

,"hab_prime_model_list.RData"
,"hab_prime_output_list.RData"

,"latmig_mod_list.RData"
,"hab.breadth_list.RData")


#load all output into memory
for(i in 1:length(all.output)){
  file.i <- paste("./MODEL_OUTPUT/",all.output[i],sep = "")
  load(file.i)
  
}
```



## Get focal AIC values

Function to get the focal AIC values
```{r}

get.focal.AIC <- function(IC.out){
 IC.out <- data.frame(model = attr(IC.out,"row.names"),
                     logLik = IC.out$logLik,
                AIC = IC.out$AIC,
                dAIC = IC.out$dLogLik,
                df = IC.out$df)
i.use <- which(IC.out$model %in% c("m1.null",
                                   "m1.year",
                                   "m6.drp.yrXtrt",
                                   "m7a.best"))

IC.out <- IC.out[i.use,]
IC.out$model <- c("null","year","year+trait","year*trait")
IC.out
}

```


## Get full AIC tables
```{r}
library(bbmle)

canopy.ob.YN.IC.out <- AICtab(canopy.ob.YN.mod.list,
            base = TRUE,
            logLik = TRUE,
            sort = F)

canopy.use.YN.IC.out <- AICtab(canopy.use.YN.mod.list,
            base = TRUE,
            logLik = TRUE,
            sort = F)

cons.priority.IC.out <- AICtab(cons.priority.mod.list,
            base = TRUE,
            logLik = TRUE,
            sort = F)

dist.sense.IC.out <- AICtab(dist.sense.mod.list,
            base = TRUE,
            logLik = TRUE,
            sort = F)
elev.mig.IC.out <- AICtab(elev.mig.mod.list,
            base = TRUE,
            logLik = TRUE,
            sort = F)
forage.guilde.2lev.IC.out <- AICtab(forage.guilde.2lev.mod.list,
            base = TRUE,
            logLik = TRUE,
            sort = F)
forage.guilde.3lev.IC.out <- AICtab(forage.guilde.3lev.mod.list,
            base = TRUE,
            logLik = TRUE,
            sort = F)
hab.breadth.IC.out <- AICtab(hab.breadth.mod.list,
            base = TRUE,
            logLik = TRUE,
            sort = F)
hab.prime.IC.out <- AICtab(hab.prime.mod.list,
            base = TRUE,
            logLik = TRUE,
            sort = F)

```


## Run null model for lat. migrants

Run null models and set up AIC
```{r}
library(lme4)
lat.mig.null <- update(lat.mig.mod.list$m7a.best, .~. - trait - year.cent - trait*year.cent)
lat.mig.yr <- update(lat.mig.null, .~. + year.cent)
```



Make AICtable
```{r}


lat.mig.IC.out <- AICtab(lat.mig.null,
                         lat.mig.yr,
                         lat.mig.mod.list$m6.drp.yrXtrt,
                         lat.mig.mod.list$m7a.best,
            base = TRUE,
            logLik = TRUE,
            sort = F)


lat.mig.IC.out <- data.frame(model = attr(lat.mig.IC.out,"row.names"),
                     logLik = lat.mig.IC.out$logLik,
                AIC = lat.mig.IC.out$AIC,
                dAIC = lat.mig.IC.out$dLogLik,
                df = lat.mig.IC.out$df)

lat.mig.IC.out$model <- c("null","year","year+trait","year*trait")


```



## Focal AIC values

Get all focal AIC values
```{r}
AIC.out <- rbind(lat.mig.IC.out
                ,get.focal.AIC(hab.prime.IC.out)
      ,get.focal.AIC(dist.sense.IC.out)
      ,get.focal.AIC(canopy.ob.YN.IC.out)
      ,get.focal.AIC(cons.priority.IC.out)
      ,get.focal.AIC(elev.mig.IC.out)
      ,get.focal.AIC(canopy.use.YN.IC.out)
,get.focal.AIC(forage.guilde.2lev.IC.out)
,get.focal.AIC(forage.guilde.3lev.IC.out)
,get.focal.AIC(hab.breadth.IC.out)
)

```


## Name table
```{r}
names(AIC.out)[1] <- "Model type"

AIC.out$model <- c("Migration status","","",""
,"Habitat preference","","",""
,"Sensitivity to disturbance ","","",""
,"Conservation priority","","",""
,"Elevational migrant","","",""
,"Obligate Canopy Use","","",""
,"Canopy Use","","",""
,"Foraging  guild-2 levels","","",""
,"Foraging  guild-3 levels","","",""
,"Habitat breadth","","",""
)

#reorder
AIC.out2 <- AIC.out[,c(6,1:5)]
```



## Save output

```{r}
write.csv(AIC.out2, file = "temp.csv",row.names = F)
```

